package com.proj.android.canyondefense.core.base;

import java.util.ArrayList;
import java.util.List;

public class Pool<T> {
	public interface IPoolObjectFactory<T> {
		public T createObject();
	}
	
	private final List<T> _freeObjects;
	private final IPoolObjectFactory<T> _factory;
	private final int _maxSize;
	
	public Pool(IPoolObjectFactory<T> factory, int maxSize) {
		this._factory = factory;
		this._maxSize = maxSize;
		this._freeObjects = new ArrayList<T>(_maxSize);
	}

	public T newObject() {
		T object = null;
		if(_freeObjects.size() == 0) {
			object = _factory.createObject();
		} else {
			object = _freeObjects.remove(_freeObjects.size() - 1);
		}
		return object;
	}
	
	public void free(T object) {
		if (_freeObjects.size() < _maxSize) {
			_freeObjects.add(object);
		}
	}
}
